package com.wondersgroup.fjzlpt.console.dao.impl.yljgry;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quick.framework.boot.repository.sql.bean.SQLBean;
import com.quick.framework.boot.repository.sql.bean.table.TableBean;
import com.quick.framework.boot.repository.sql.jdbcTemplate.impl.BaseJTDAOImpl;
import com.quick.framework.util.FormatDate;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgry.YljgryPracticeMultiDAO;
import com.wondersgroup.fjzlpt.console.dao.inter.yljgry.YljgryZybjDAO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 多点执业
 * Created by lion on 2017/6/2.
 */
@Repository
public class YljgryPracticeMultiDAOImpl extends BaseJTDAOImpl implements YljgryPracticeMultiDAO {
    @Override
    @Autowired
    @Qualifier("TB_ZD_YLJGRY_PRACTICE_MULTI")
    public void setTb(TableBean tb) {
        super.setTb(tb);
    }

    public JSONArray queryByUserId(String userId){
        StringBuilder sbud = new StringBuilder("select a.XH, a.PRACTICE_JGID as PTJGID, a.KSDM, a.QYKSRQ, a.QYJSRQ, b.YLJGMC, c.KSMC from \n")
                .append(this.tb.getTableName()).append(" a \n")
                .append("left join tb_zd_yljgxx b on a.PRACTICE_JGID=b.PTJGID \n")
                .append("left join tb_zd_ksxx c on a.KSDM=c.KSDM and a.PRACTICE_JGID=c.PTJGID \n")
                .append(" where a.userid=? order by a.XH");
        SQLBean sb = new SQLBean(sbud.toString(), new Object[]{userId});
        return this.queryList(sb);
    }

    public void insertBatch(String userId, JSONArray ary){
        Iterator iter = ary.iterator();
        while(iter.hasNext()){
            JSONObject json = (JSONObject)iter.next();
            json.put("USERID", userId);
            String QYKSRQ = json.getString("QYKSRQ");
            if(StringUtils.isEmpty(QYKSRQ)){
                json.put("QYKSRQ", null);
            }else{
                json.put("QYKSRQ", FormatDate.getDateY_M_D(QYKSRQ));
            }

            String QYJSRQ = json.getString("QYJSRQ");
            if(StringUtils.isEmpty(QYJSRQ)){
                json.put("QYJSRQ", null);
            }else{
                json.put("QYJSRQ", FormatDate.getDateY_M_D(QYJSRQ));
            }
        }
        this.insertBatch(ary.toJavaList(Map.class));
    }

    public void deleteByUserId(String userId){
        StringBuilder sbud = new StringBuilder("delete from ")
                .append(this.tb.getTableName())
                .append(" where USERID=?");
        SQLBean sb = new SQLBean(sbud.toString(), userId);
        this.update(sb);
    }
}
